import { utilParseTime } from "@/assets/utils";
export function getModulePropFn({ props, emit, remoting }: IFormItemRModulePropFn) {
  let item = props.item;
  let elInputCommon, elSelectCommon, elDatePickerCommon;
  if (item.type == "password" || item.type == "input" || item.type == "number" || item.type == "textarea") {
    elInputCommon = {
      maxlength: item.maxlength,
      disabled: item.disabled,
      clearable: true,
      placeholder: item.placeholder ?? `请输入${item.label}`,
      style: item.style
    };
    if (item.type == "password") {
      let obj: IFormItemPItemPasswordP = {
        type: item.type,
        showPassword: item.showPassword,
        ...elInputCommon
      };
      return obj;
    }
    if (item.type == "number" || item.type == "input") {
      let obj: (IFormItemPItemInputP | IFormItemPItemNumberP) & { clearable: boolean } = { ...elInputCommon, type: item.type };
      return obj;
    }
    if (item.type == "textarea") {
      let obj: IFormItemPItemTextareaP = {
        type: item.type,
        showWordLimit: item.showWordLimit,
        rows: item.rows || 5,
        autosize: item.autosize,
        ...elInputCommon
      };
      return obj;
    }
  }
  if (item.type == "treeSelect") {
    return {
      data: item.values || []
    };
  }
  if (item.type == "tree") {
    return {
      defaultExpandAll: item.defaultExpandAll,
      props: item.props,
      data: item.values || [],
      showCheckbox: true,
      nodeKey:item.nodeKey||'id'
    };
  }
  if (item.type == "select" || item.type == "selectRemote" || item.type == "selectCreate") {
    elSelectCommon = {
      multiple: item.multiple,
      clearable: true,
      disabled: item.disabled,
      placeholder: item.placeholder ?? `请输入${item.label}`
    };
    type IFormItemPItemSelectPts = IFormItemPItemSelectP & { clearable: boolean };
    if (item.type == "select") {
      let obj: IFormItemPItemSelectPts = elSelectCommon;
      return obj;
    }
    type IFormItemPItemSelectRemotePts = IFormItemPItemSelectPts & { remote: boolean; filterable: boolean; remoteMethod: ({ e, item }: { e: any; item: any }) => Promise<void>; remoteShowSuffix: boolean; defaultFirstOption: boolean };
    if (item.type == "selectRemote") {
      let obj: IFormItemPItemSelectRemotePts = {
        //启用远程搜索，需filterable和remote为true
        remote: true,
        filterable: true,
        // loading:remoting,
        remoteMethod: async ({ e, item }: { e: any; item: any }) => {
          // remoting = true;
          let url = item.url + e;
          let res = await hfoRequest.get({ url: url, params: { ...item?.params } });
          if (res) {
            emit("emitChangeProps", { e: res.data.content, propsName: ["formOpt", "items", props.itemKey, "values"] });
            // remoting = false;
          }
        },
        remoteShowSuffix: true, //是否显示远程搜索的图标
        defaultFirstOption: true, //是否在输入框按下回车时，选择第一个匹配项。
        ...elSelectCommon
      };
      return obj;
    }
    if (item.type == "selectCreate") {
      let obj: IFormItemPItemSelectPts & { filterable: boolean; allowCreate: boolean; defaultFirstOption: boolean } = {
        filterable: true,
        allowCreate: true,
        defaultFirstOption: true, //是否在输入框按下回车时，选择第一个匹配项。
        ...elSelectCommon
      };
      return obj;
    }
  }
  if (item.type == "daterange" || item.type == "datetimerange" || item.type == "monthrange" || item.type == "date" || item.type == "datetime") {
    elDatePickerCommon = {
      defaultTime: item.defaultTime,
      clearable: true,
      valueFormat: item.value_format
    };
    type IFormItemPItemDatePts = IFormItemPItemDateP & { rangeSeparator: string; startPlaceholder: string; endPlaceholder: string; shortcuts: any };
    if (item.type == "daterange") {
      let obj: IFormItemPItemDatePts = {
        rangeSeparator: "至",
        startPlaceholder: "开始时间",
        endPlaceholder: "结束时间",
        shortcuts: item.shortcuts || [
          { text: "已过期", value: [utilParseTime(new Date().getTime()), utilParseTime(new Date().getTime())] },
          { text: "近5天", value: [utilParseTime(new Date().getTime()), utilParseTime(new Date().getTime() + 3600 * 1000 * 24 * 5)] },
          { text: "近15天", value: [utilParseTime(new Date().getTime()), utilParseTime(new Date().getTime() + 3600 * 1000 * 24 * 15)] }
        ],
        ...elDatePickerCommon
      };
      return obj;
    }
    if (item.type == "datetimerange") {
      let obj: IFormItemPItemDatePts = {
        shortcuts: item.shortcuts,
        rangeSeparator: "至",
        startPlaceholder: "开始时间",
        endPlaceholder: "结束时间",
        ...elDatePickerCommon
      };
      return obj;
    }
    if (item.type == "monthrange") {
      let obj: IFormItemPItemDatePts = {
        shortcuts: item.shortcuts,
        rangeSeparator: "至",
        startPlaceholder: "开始时间",
        endPlaceholder: "结束时间",
        ...elDatePickerCommon
      };
      return obj;
    }
    if (item.type == "date") {
      let obj: IFormItemPItemDateP & { shortcuts: any; placeholder: string } = {
        shortcuts: item.shortcuts,
        placeholder: "请选择时间",
        ...elDatePickerCommon
      };
      return obj;
    }
    if (item.type == "datetime") {
      let obj: IFormItemPItemDateP & { shortcuts: any; placeholder: string } = {
        shortcuts: item.shortcuts,
        placeholder: "请选择时间",
        ...elDatePickerCommon
      };
      return obj;
    }
  }
  type IFormItemPItemtime_pickerPts = IFormItemPItemTime_pickerP & {
    placeholder: string;
    isRange?: boolean;
    rangeSeparator: string;
    startPlaceholder: string;
    endPlaceholder: string;
    valueFormat: string;
    format: string;
  };
  if (item.type == "time_picker") {
    let obj: IFormItemPItemtime_pickerPts = {
      placeholder: item.placeholder ?? `请选择${item.label}`,
      isRange: item.is_range,
      rangeSeparator: "至",
      startPlaceholder: "开始时间",
      endPlaceholder: "结束时间",
      valueFormat: item.value_format || "HH:mm",
      format: item.format || "HH:mm"
    };
    return obj;
  }
  if (item.type == "checkbox" || item.type == "radio") {
    let obj = {
      disabled: item.disabled
    };
    return obj;
  }
  if (item.type == "time") {
    let obj: IFormItemPItemTimeP = {
      start: item.start,
      end: item.end,
      placeholder: item.placeholder ?? `请输入${item.label}`
    };
    return obj;
  }
  if (item.type == "cascader") {
    let obj: IFormItemPItemCascaderP & { placeholder: string; clearable: boolean } = {
      props: item.props,
      style: item.style,
      options: item.options,
      placeholder: "请选择",
      clearable: true
    };
    return obj;
  }
  if (item.type == "uAudio" || item.type == "uVideo" || item.type == "uImg" || item.type == "uImgs" || item.type == "uFile" || item.type == "uGetExcelData") {
    let obj: IUploadP = {
      item: item
    };
    return obj;
  }
}
export const getFormItemPropFn = (item: IFormItemPItem, prop: string) => {
  return {
    rules: item.hide || !item.hasrules ? null : item.rules ? item.rules : [{ required: true, message: `请输入${item.label}`, trigger: "blur" }].concat(item.otherRules ? item.otherRules : []),
    style: item.formStyle ? JSON.parse(item.formStyle) : "{}",
    label: item.label ? item.label + ":" : "",
    prop: prop
  };
};
